package com.allwees.bs.module.oms.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.module.oms.entity.OrderRefundItem;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author yangylsky
 * @since 2019-08-21 09:26
 */
public interface OrderRefundItemDao extends BaseDao<OrderRefundItem> {

    default OrderRefundItem findByRefundUuid(String orderRefundUuid) {
        if (StringUtils.isBlank(orderRefundUuid)) {
            return null;
        }
        QueryWrapper<OrderRefundItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_refund_uuid", orderRefundUuid);
        List<OrderRefundItem> list = selectList(wrapper);
        return list != null ? list.get(0) : null;
    }

    default OrderRefundItem findByOrderItemUuid(String orderItemUuid) {
        if (StringUtils.isBlank(orderItemUuid)) {
            return null;
        }
        QueryWrapper<OrderRefundItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_item_uuid", orderItemUuid);
        List<OrderRefundItem> list = selectList(wrapper);
        if (list == null) {
            return null;
        }
        list.sort((o1, o2) -> o2.getCreatedAt().compareTo(o1.getCreatedAt()));

        return list.get(0);
    }
}
